package com.mf.controller;

import com.alibaba.fastjson2.JSON;
import com.mf.config.RabbitMqConfig;
import com.mf.entity.request.PermissionSaveRequest;
import com.mf.service.IPermissionService;
import io.github.heollhai.common.config.exceptions.GraceException;
import io.github.heollhai.common.entity.bo.PermissionBo;
import io.github.heollhai.common.entity.request.SocketMessageRequest;
import io.github.heollhai.common.entity.request.publicRequest.PublicBindingIdRequest;
import io.github.heollhai.common.entity.request.publicRequest.PublicUpdateStatusRequest;
import io.github.heollhai.common.entity.vo.permissionTree.RouteVo;
import io.github.heollhai.common.enums.EnableOrDisableEnum;
import io.github.heollhai.common.enums.EnumSocketType;
import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.utils.MybatisPlusUtils;
import io.github.heollhai.common.utils.VerifyUtils;
import io.github.heollhai.common.utils.enums.EnumUtils;
import io.github.heollhai.common.utils.tree.TreeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 权限表
 *
 * @author mf
 * @since 2024-08-06
 */
@RestController
@RequestMapping("/auth/permission/v1")
@Tag(name = "权限管理")
@Slf4j
public class PermissionController {
    @Resource
    private IPermissionService permissionService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Operation(summary = "发送mq消息测试")
    @GetMapping("send")
    public Result<String> send() {
        log.info("发送mq消息测试");
        final SocketMessageRequest messageRequest = new SocketMessageRequest();
        messageRequest.setMessage("你好啊");
        messageRequest.setType(EnumSocketType.SYSTEM);
        messageRequest.setMsgTitle("消息测试");
        final String jsonString = JSON.toJSONString(messageRequest);
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, "basic.sendSocket.auth", jsonString);
        return Result.success();
    }

    @Operation(summary = "新增|编辑权限")
    @PostMapping("save")
    public Result<Boolean> save(@Valid @RequestBody PermissionSaveRequest permissionSaveRequest) {
        PermissionBo permissionBo = new PermissionBo();
        BeanUtils.copyProperties(permissionSaveRequest, permissionBo);
        if(VerifyUtils.isEmpty(permissionSaveRequest.getId())){
            return Result.success(permissionService.submit(permissionBo));
        }

//        // 不能修改父级id
//        permissionBo.setPid(null);
        return Result.success(permissionService.updateById(permissionBo));
    }

    @Operation(summary = "删除权限")
    @GetMapping("deleteById/{id}")
    public Result<Integer> deleteById(@PathVariable("id") Integer id) {
        return Result.success(permissionService.deleteById(id));
    }

    @Operation(summary = "修改权限状态")
    @PostMapping("updateStatus")
    public Result<Boolean> updateStatus(@Valid @RequestBody PublicUpdateStatusRequest<Integer> updateStatusRequest) {
        if(EnumUtils.existsInEnum(EnableOrDisableEnum.class, updateStatusRequest.getStatus())){
            GraceException.display("状态异常");
        }
        PermissionBo permissionBo = MybatisPlusUtils.changeDataClass(updateStatusRequest, PermissionBo.class);
        return Result.success(permissionService.updateById(permissionBo));
    }
    
    @Operation(summary = "根据角色id绑定权限")
    @PostMapping("permissionIdsBindingRoleId")
    public Result<Integer> permissionIdsBindingRoleId(@Valid @RequestBody PublicBindingIdRequest bindingIdRequest) {
        return Result.success(permissionService.permissionIdsBindingRoleId(bindingIdRequest));
    }

    @Operation(summary = "查询所有权限")
    @GetMapping("findListPermission")
    public Result<List<PermissionBo>> findListPermission() {
        return Result.success(permissionService.findListPermission());
    }

    @Operation(summary = "根据角色id查询绑定的权限Id集合")
    @GetMapping("findListByRoleId")
    public Result<List<Integer>> findListByRoleId(@RequestParam("id") Integer roleId) {
        return Result.success(permissionService.findListByRoleId(roleId));
    }


    @Operation(summary = "查询菜单所有权限(树结构)")
    @GetMapping("findTreeMenuList")
    public Result<List<RouteVo>> findTreeMenuList() {
        return Result.success(permissionService.findTreeMenuList());
    }

    @Operation(summary = "查询所有权限(树结构)")
    @GetMapping("findTreeList")
    public Result<List<RouteVo>> findTreeList() {
        return Result.success(permissionService.findTreeList());
    }
    
    @Operation(summary = "查询平铺后的树结构")
    @GetMapping("flattenTree")
    public Result<List<RouteVo>> flattenTree() {
        List<RouteVo> treeMenuList = permissionService.findTreeMenuList();
        return Result.success(TreeUtils.flattenTree(treeMenuList));
    }

    @Operation(summary = "根据父级id查询菜单权限")
    @GetMapping("findByPid/{pid}")
    public Result<List<PermissionBo>> findByPid(@PathVariable("pid") Integer pid) {
        return Result.success(permissionService.findByPid(pid));
    }
}
